package cn.wolfcode.web.interceptor;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.util.UserContext;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class CheckLoginInterceptor implements HandlerInterceptor {
    // 检查是否登录
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 通过工具类 => 获取登录员工对象 本质 从 session中 => 拿到员工对象
        Employee employee = UserContext.getEmployee();
        // 没有登录
        if (employee == null) {
            // 重定向到 登录页面
            response.sendRedirect("/login.html");
            // 拦截
            return false;
        }
        // 登录成功 放行
        return true;
    }
}
